package com.cloud.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import com.cloud.train.business.domain.TrainCarriage;
import com.cloud.train.business.domain.TrainCarriageExample;
import com.cloud.train.business.enums.SeatColEnum;
import com.cloud.train.business.mapper.TrainCarriageMapper;
import com.cloud.train.business.req.TrainCarriageQueryReq;
import com.cloud.train.business.req.TrainCarriageSaveReq;
import com.cloud.train.business.req.TrainSeatSaveReq;
import com.cloud.train.business.resp.TrainCarriageQueryResp;
import com.cloud.train.common.exception.BusinessException;
import com.cloud.train.common.exception.BusinessExceptionEnum;
import com.cloud.train.common.resp.CommonResp;
import com.cloud.train.common.resp.PageResp;
import com.cloud.train.common.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TrainCarriageService {
    @Resource
    private TrainCarriageMapper trainCarriageMapper;
    public void save(TrainCarriageSaveReq req) {
        List<SeatColEnum> seatColEnums = SeatColEnum.getColsByType(req.getSeatType());
        req.setColCount(seatColEnums.size());
        req.setSeatCount(req.getRowCount() * req.getColCount());
        DateTime dateTime = new DateTime();
        TrainCarriage trainCarriage = BeanUtil.copyProperties(req, TrainCarriage.class);
        if (ObjectUtil.isNull(trainCarriage.getId())){
            TrainCarriage trainCarriage1 = selectByUnique(req.getTrainCode(), req.getIndex());
            if (ObjectUtil.isNotEmpty(trainCarriage1)){
                throw new BusinessException(BusinessExceptionEnum.BUSINESS_TRAIN_CARRIAGE_INDEX_UNIQUE_ERROR);
            }
            trainCarriage.setId(SnowUtil.getSnowflakeNextId());
            trainCarriage.setCreateTime(dateTime);
            trainCarriage.setUpdateTime(dateTime);
            trainCarriageMapper.insert(trainCarriage);
        }else{
            trainCarriage.setUpdateTime(dateTime);
            trainCarriageMapper.updateByPrimaryKey(trainCarriage);
        }
    }
    private TrainCarriage selectByUnique(String trainCode, Integer index) {
        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        trainCarriageExample.createCriteria()
                .andTrainCodeEqualTo(trainCode)
                .andIndexEqualTo(index);
        List<TrainCarriage> list = trainCarriageMapper.selectByExample(trainCarriageExample);
        if (CollUtil.isNotEmpty(list)) {
            return list.get(0);
        } else {
            return null;
        }
    }

    public CommonResp<Object> delete(long id) {
        int i = trainCarriageMapper.deleteByPrimaryKey(id);
        CommonResp<Object> objectCommonResp = new CommonResp<>();
        if (i <= 0){
            objectCommonResp.setMessage("删除失败，未知错误");
            return objectCommonResp;
        }
        else {
            return objectCommonResp;
        }
    }

    public PageResp<TrainCarriageQueryResp> queryList(TrainCarriageQueryReq req) {
        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        trainCarriageExample.setOrderByClause("train_code asc, `index` asc");
        TrainCarriageExample.Criteria criteria = trainCarriageExample.createCriteria();
        if (ObjectUtil.isNotEmpty(req.getTrainCode())){
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }
        PageHelper.startPage(req.getPage(),req.getSize());
        List<TrainCarriage> trainCarriages = trainCarriageMapper.selectByExample(trainCarriageExample);
        PageInfo<TrainCarriage> trainCarriagePageInfo = new PageInfo<>(trainCarriages);
        List<TrainCarriageQueryResp> trainCarriageQueryResps = BeanUtil.copyToList(trainCarriages, TrainCarriageQueryResp.class);
        PageResp<TrainCarriageQueryResp> trainCarriageQueryRespPageResp = new PageResp<>();
        trainCarriageQueryRespPageResp.setTotal(trainCarriagePageInfo.getTotal());
        trainCarriageQueryRespPageResp.setList(trainCarriageQueryResps);
        return trainCarriageQueryRespPageResp;
    }
    public List<TrainCarriage> selectByTrainCode(String trainCode) {
        TrainCarriageExample trainCarriageExample = new TrainCarriageExample();
        trainCarriageExample.setOrderByClause("`index` asc");
        TrainCarriageExample.Criteria criteria = trainCarriageExample.createCriteria();
        criteria.andTrainCodeEqualTo(trainCode);
        return trainCarriageMapper.selectByExample(trainCarriageExample);
    }
}
